﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PesticideManage.API.Data;
using PesticideManage.Domain.AggregatesModel.PesticideAggregate;

namespace PesticideManage.API.Controllers;
[Route("api/[controller]")]
[ApiController]
public class PesicideInfoController : ControllerBase
{
    private readonly PesticideContext _context;

    public PesicideInfoController(PesticideContext context)
    {
        _context = context;
    }
    [AllowAnonymous]
    [HttpGet("{id}")]
    public async Task<ActionResult<Pesticide>> GetPesticide(string id)
    {
        if (_context.Pesticides == null)
        {
            return NotFound();
        }
        string traceableId = id[..25];
        var InvalidQRCode = await _context.InvalidQRCodes.FirstOrDefaultAsync(i => i.TraceableId == traceableId);
        if (InvalidQRCode != null)
        {
            return NotFound();
        }
        int pesticideId = int.Parse(id[11..15]);
        var pesticide = await _context.Pesticides.FirstOrDefaultAsync(p => p.Id == pesticideId);

        if (pesticide == null)
        {
            return NotFound();
        }

        return pesticide;
    }

}
